

#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <QDebug>

#include "SimpleByteBuffer.h"
#include "ByteBuffer.h"
using namespace std;


struct Test{
    Test() {}
    int i,j,k;
    char c;

    friend ByteBuffer& operator<<(ByteBuffer& os, const Test& t)
    {
        os << t.i<< t.j<< t.k<< t.c ;
        return os;  //要有返回值
    }

    friend ByteBuffer& operator>>(ByteBuffer& is, Test& t)
    {
        is >> t.i>> t.j>>t.k>> t.c ;
        return is;  //要有返回值
    }

    friend ostream& operator<<(ostream& os, const Test& t)
    {
        os << t.i << "," << t.j << ","<< t.k<< ","<< t.c ;
        return os;  //要有返回值
    }
};

void byteBufferDemo()
{
    {
        Test t,t1,t2;
        ByteBuffer buf;
        t.i=10;
        t.j=11;
        t.k=-1;
        t.c='a';

        buf<<t;
        buf>>t1;
        cout<<t1<<endl;

         try {
            buf>>t2;
            cout<<t2<<endl;
        }catch (ByteBufferException e) {
            cout<<"ByteBufferException";
           // debugx("ByteBufferException");
        }catch (...) {
           // debugx("unknow error");
            cout<<"unknow error";
        }


    }
    if(1<0)

    {
        qDebug("platform_little_endian: %s",platform_little_endian?"true":"false");
        ByteBuffer buf;
        const char *p="hello";
        buf<<p;
        qDebug("p ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        char a0='a';
        const char a1='b';
        const signed char a2='c';
        const unsigned char a3='d';
        const static char a4='e';
        char a5=-10;
        uint8_t b=98;
        int16_t c=100;
        int32_t c1=101;
        uint32_t c2=102;
        int64_t c3=103;
        uint64_t c4=104;

        buf<<a0;
        qDebug("a0 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf<<a1;
        qDebug("a1 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf<<a2;
        qDebug("a2 ByteBuffer Size: %llu",buf.size());
        buf<<a3;
        qDebug("a3 ByteBuffer Size: %llu",buf.size());
        buf<<a4;
        qDebug("a4 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf<<a5;
        qDebug("a5 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf<<b;
        qDebug("b ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf<<c;
        qDebug("c ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();

        buf<<c1;
        qDebug("c1 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();

        buf<<c2;
        qDebug("c2 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();

        buf<<c3;
        qDebug("c3 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();

        buf<<c4;
        qDebug("c4 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();

        std::string hello;
        buf>>hello;
        qDebug()<<hello;
        qDebug("hello ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        char a01,a11,a21,a31,a41,a51;
        buf>>a01;
        qDebug()<<a01;
        //     qDebug("a01 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf>>a11;
        qDebug()<<a11;
        //    qDebug("a11 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf>>a21;
        qDebug()<<a21;
        //     qDebug("a21 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf>>a31;
        qDebug()<<a31;
        //    qDebug("a31 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf>>a41;
        qDebug()<<a41;
        //    qDebug("a41 ByteBuffer Size: %llu",buf.size());
        qDebug()<<buf.textlike();
        buf>>a51;
        qDebug("a51  : %d",a51);
        //      qDebug("a51 ByteBuffer Size: %llu",buf.size());
        //      qDebug()<<buf.textlike();

        uint8_t b1=0;
        int16_t c01=0;
        int32_t c11=0;
        uint32_t c21=0;
        int64_t c31=0;
        uint64_t c41=0;

        buf >> b1;
        qDebug() << b1;
        buf >> c01;
        qDebug() << c01;
        buf >> c11;
        qDebug() << c11;
        buf >> c21;
        qDebug() << c21;
        buf >> c31;
        qDebug() << c31;
        buf >> c41;//104
        qDebug() << c41;
        //     qDebug("b1 ByteBuffer Size: %llu", buf.size());
        qDebug() << buf.textlike();




    }

}

void strBufDemo()
{
    if(1<0)

    {
        /*
         数值没有改变，但对数值的解释，发生了变化

当char类型的值为正时，转换后的值为原值。
当char类型为负时，原始值的符号位会转为数据位，即结果值为原始值补码代表的无符号值。
具体计算公式为，
结果值=256+原始值。
*/
        char si = -10;
        unsigned char usi = si;
        printf("char: %d,unsigned char:%d \n",si,usi);
        printf("char: %c,unsigned char:%c \n",si,usi);
        char z = 'z';
        char Z = 'Z';
        printf("z: %c, int :%d \n",z,z);
        printf("Z: %c, int :%d \n",Z,Z);


        printf("\n    static_cast \n");
        char c = 'a';
        unsigned char uc = static_cast<unsigned char>(c);
        printf("char: %c,unsigned char:%c \n",c,uc);
        uc =129;
        c = static_cast<char>(uc);
        printf("129 char: %c,unsigned char:%c \n",c,uc);
        printf("129 char: %d,unsigned char:%d \n",c,uc);


        c = -20;
        uc = static_cast<unsigned char>(c);
        printf("-20 char: %c,unsigned char:%c \n",c,uc);
        printf("-20 char: %d,unsigned char:%d \n",c,uc);


        //数值没有改变，但对数值的解释，发生了变化
        char cAry[10];
        unsigned char *pUC =(unsigned char *)&cAry;
        cAry[0]='a';
        cAry[1]=-10;
        printf("pUC 0: %d, 1:%d \n",pUC[0],pUC[1]);
        char *pC =(char *)pUC;
        printf("pC 0: %d, 1:%d \n",pC[0],pC[1]);
    }


    if(1<0)
    {
        SimpleByteBuffer byBuf;

        const char *p="hello";
        byBuf.Put((uchar *)p,5);

        qDebug("CanReadSize: %u",byBuf.CanReadSize());


        stringbuf buf;
        ostream out(&buf);

        out<<"hello";
        int i = 1234;
        long long ll = 1234567;
        out << i << endl << ll << endl;


        float f=1.1;
        out << f << endl;
        char c = 'X';
        out.put('c').put('=').put(c).put('\n');


        out.write("aaa\n", 4);


        qDebug().noquote() <<buf.str();
    }


    return;

     {
         std::stringbuf buffer;


         buffer.str("This is test stringbuf");
         cout << buffer.str() << endl;       //This is test stringbuf

         streamsize size = buffer.in_avail();//long long可读字符个数=22
         cout << "1.1.size=" << size << endl;

         char arr[10] = {0}, arr1[23] = {0};
         buffer.sgetn(arr, 10);             //得到n个字符 This is te
         size = buffer.in_avail();          //12
         cout << "1.2.size=" << size << endl;

         puts(arr);                         //从开头放10个字符,尾部换行
         //gets_s(arr1, 23);                //不要使用
         //cout << "arr1=" << arr1 << endl; //This is te孂?

         buffer.sgetn(arr1, 22);
         cout << "1.3.arr1=" << arr1 << endl;

         buffer.str("This is test stringbuf");
         buffer.sgetn(arr1, 22);
         cout << "1.4.arr1=" << arr1 << endl;//This is test stringbuf

         buffer.str("This is test stringbuf");
         buffer.sputn("Tom", 3);
         char arr2[25] = { 0 };
         buffer.sgetn(arr2, 25);
         cout << "1.5.arr2=" << arr2 << endl;//Toms is test stringbuf

         buffer.str("This is test stringbuf");
         cout << "1.6.";
         while (buffer.snextc() != EOF)     //移动到下一个位置
         {
             char cc = buffer.sgetc();      //返回当前位置字符
             cout << cc << ";";
         }
         cout << endl;/*h;i;s; ;i;s; ;t;e;s;t; ;s;t;r;i;n;g;b;u;f; */
         size = buffer.in_avail();
         cout << "1.7.size=" << size << endl;//0

         buffer.str("This is test stringbuf");
         cout << "1.8.";
         while (buffer.sgetc() != EOF)      //移动到下一个位置
         {
             char cc = buffer.sbumpc();     //返回当前位置字符
             cout << cc << ";";
         }
         cout << endl;
         size = buffer.in_avail();/*T; h; i; s; ; i; s; ; t; e; s; t; ; s; t; r; i; n; g; b; u; f;*/
         cout << "1.9.size=" << size << endl;//0



         buffer.str("This is test stringbuf");
     }


}
